package com.example.bausip;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class TasksActivity extends Activity {
	ArrayList<NameEntity> tasks;
	ArrayList<Item> taskItems;
	MyCustomAdapter dataAdapter;
	int course_id,student_id,instructor_id;
	String condition;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_majors);
		Bundle extras = getIntent().getExtras();
		if(extras!=null)
		{
			course_id = extras.getInt("course_id");
			condition = "?course_id="+course_id;
		}
		else
		{
			if(sip.isStudent());
			{
				student_id = sip.getUserId();
				condition = "?student_id="+student_id;
			}
		}
		getTasks() ;
	}
	
	public void getTasks() {
		Log.d("ray", "getting: " );
		
		String serverURL = new myURL("read_tasks.php"+condition,true).getURL();
		RZHelper p = new RZHelper(serverURL, this, "setTasks", true);
		p.get();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.question, menu);
		return true;
	}
	public void setTasks(String s, String error) {
		Log.d("ray", "reply: " + s);
		NameEntity ne = null;
		ArrayList<Task> myTs = new APIManager().getTasks(s);
		tasks = new ArrayList<NameEntity>();
		for(int i =0;i<myTs.size();i++)
		{
			ne = new NameEntity(myTs.get(i).getId(), myTs.get(i).toString(), "course");
			ne.setDescription(myTs.get(i).getDescription());
			tasks.add(ne);
		}
		taskItems = new ArrayList<Item>();
		ListView listView = (ListView) findViewById(R.id.list);

		listView.setTextFilterEnabled(true);
		if (tasks.size() == 0) {
			taskItems.add(new Item(0, "", getString(R.string.empty_list)));
		} else {
			for (int i = 0; i < tasks.size(); i++) {
				Item it = new Item(tasks.get(i).getId(), "",
						tasks.get(i).toString());
				it.setCode(tasks.get(i).getDescription());
				taskItems.add(it);
			}
			registerForContextMenu(listView);
			dataAdapter = new MyCustomAdapter(this, R.layout.child_question_list,
					taskItems);
			SharedMenu.adapter = dataAdapter;
			listView.setAdapter(dataAdapter);
			listView.setOnItemClickListener(new OnItemClickListener() {

				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					if (dataAdapter.tmpList.size() > 0) {
						Intent i;
						if(sip.isStudent())
						{
							i = new Intent(getBaseContext(),StudentTaskActivity.class);
						}
						else
						{
							i = new Intent(getBaseContext(),TaskActivity.class);
						}
						i.putExtra("task_id", dataAdapter.tmpList.get(position).getId());
						startActivity(i);

					}
				}
			});
		}
	}
}
